#include <istream>
#include <fstream>
#include "contacts.pb.h"

void PrintInput(const contacts::Contacts contacts)
{
    for(int i = 0;i<contacts.person_size();i++)
    {
        const contacts::People& person = contacts.person(i);
        std::cout << "-------------------联系人：" << i+1 << "---------------------" << std::endl;
        std::cout << "联系人姓名：" << person.name() << std::endl;
        std::cout << "联系人年龄：" << person.age() << std::endl;
        for(int j = 0;j<person.phone_size();j++)
        {
            const contacts::People_Phone phone = person.phone(j);
            std::cout << "联系人电话" << j+1  << ": "<< phone.num() << "   ";
            std::cout << "电话类型"  << ": ("<< People_Phone_PhoneType_Name(phone.type()) << ")" << std::endl; 
        }
        //has_data: 判断any对象是不是一个已经被赋值的数据   data():返回any对象，Is(): 模板函数，判断是否有这个类型
        if(person.has_data() && person.data().Is<contacts::Address>())
        {
            contacts::Address address;
            person.data().UnpackTo(&address);
            if(!address.home_addr().empty())
                std::cout << "联系人家庭住址: " << address.home_addr() << std::endl;
            if(!address.unit_addr().empty())
                std::cout << "联系人单位住址: " << address.unit_addr() << std::endl;
        }

        switch(person.other_contact_case())
        {
            case contacts::People::OtherContactCase::kQq:
                std::cout << "联系人的qq是: " << person.qq() << std::endl;
                break;
            case contacts::People::OtherContactCase::kWechat:
                std::cout << "联系人的微信是: " << person.wechat() << std::endl;
                break;
            default:
                break;   
        }


        for(auto& e : person.remarks())
        {
            std::cout << "备注: " << std::endl;
            std::cout << "    " << "主题: " << e.first << " 内容: " << e.second;
        }
        std::cout << std::endl;
    }
}
int main()
{
    std::ifstream input("contacts.bin",std::ios::in | std::ios::binary);
    contacts::Contacts contacts;
    if(!contacts.ParseFromIstream(&input))
    {
        std::cerr << "Parse error" << std::endl;
        input.close();
        return -1;
    }


    PrintInput(contacts);
}